package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.MemberDto;
import io.geekidea.boot.shop.entity.Member;
import io.geekidea.boot.shop.mapper.MemberMapper;
import io.geekidea.boot.shop.query.app.AppMemberQuery;
import io.geekidea.boot.shop.query.web.MemberQuery;
import io.geekidea.boot.shop.service.base.api.MemberService;
import io.geekidea.boot.shop.vo.app.AppMemberVo;
import io.geekidea.boot.shop.vo.web.MemberVo;
import io.geekidea.boot.user.entity.User;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户信息 服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addMember(MemberDto dto) {
        Member member = new Member();
        BeanUtils.copyProperties(dto, member);
        return save(member);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateMember(MemberDto dto) {
        Long id = dto.getId();
        Member member = getById(id);
        if (member == null) {
            throw new BusinessException("用户信息不存在");
        }
        BeanUtils.copyProperties(dto, member);
        return updateById(member);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteMember(String id) {
        return removeById(id);
    }

    @Override
    public MemberVo getMemberById(String id) {
        return memberMapper.getMemberById(id);
    }

    @Override
    public Paging<MemberVo> getMemberPage(MemberQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<MemberVo> list = memberMapper.getMemberPage(query);
        Paging<MemberVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppMemberVo getAppMemberById(String id) {
        return memberMapper.getAppMemberById(id);
    }

    @Override
    public Paging<AppMemberVo> getAppMemberPage(AppMemberQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppMemberVo> list = memberMapper.getAppMemberPage(query);
        Paging<AppMemberVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public Member getUserByOpenid(String openid) {
        LambdaQueryWrapper<Member> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Member::getOpenid, openid);
        return getOne(wrapper);
    }
}
